﻿using SmartSapce.ServiceLocation;
using SmartSpace.Web;
using SmartSpace.Workflow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp.Modules.Workflow.Pages
{
    public partial class Workflow_View : Page
    {
        public string RefModule { get; private set; }
        public Guid RefId { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefModule = Request.GetString("RefModule");
            RefId = Request.GetGuid("RefId");

            _ctlPanelProcessing.Visible = false;
            // 获得数据的流程信息
            IWorkflowService wfService = ServiceLocator.Get<IWorkflowService>();
            ListResult<WorkflowStep> steps = wfService.GetActiveSteps(RefModule, RefId);

            SingleResult<WorkflowInstance> wfInstance = wfService.GetInstance(RefModule, RefId);
            ActivityMetadata activity = wfService.GetActivity(wfInstance.Data.CurActivityId).Data;

            if (CheckApprover(steps.Data))
            {
                _ctlStatus1.InnerText = string.Format("单据目前状态:{0}", activity.Caption);
                _ctlStatus2.InnerText = string.Format("单据目前状态:{0}", activity.Caption);
                if (steps.IsSuccess)
                {
                    for (int iStep = 0; iStep < steps.Data.Count; iStep++)
                    {
                        if (steps.Data[iStep].Receiver == User.Identity.Name)
                        {
                            Guid activityId = steps.Data[iStep].ActivityId;
                            ListResult<EventMetadata> events = wfService.GetEvents(activityId);
                            for (int index = 0; index < events.Data.Count; index++)
                            {
                                Button btn = new Button();
                                btn.ID = string.Format("btn_{0}", events.Data[index].Id);
                                btn.Text = events.Data[index].Caption;
                                btn.CommandArgument = events.Data[index].Id.ToString();
                                btn.Click += btn_Click;
                                _ctlEventContainer.Controls.Add(btn);
                            }
                        }
                    }
                }
            }
            else
            {
                _ctlStatus1.InnerText = string.Format("单据目前状态:{0}", activity.Caption);
                _ctlStatus2.InnerText = string.Format("单据目前状态:{0}", activity.Caption);
                _ctlPanelProcessing.Visible = true;
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            Guid eventId = new Guid(btn.CommandArgument);
            Response.Redirect(string.Format("./Workflow-Drive.aspx?RefModule={0}&RefId={1}&EventId={2}",RefModule, RefId, eventId));
        }

        private bool CheckApprover(List<WorkflowStep> steps)
        {
            bool isApprover = false;
            for (int index = 0; index < steps.Count; index++)
            {
                if (steps[index].Receiver == User.Identity.Name)
                {
                    isApprover = true;
                    break;
                }
            }
            return isApprover;
        }
    }
}